package com.firemoss.ui.blanket.loadingSpinnerClasses
{
	import com.firemoss.ui.blanket.AbstractBlanketOverlay;
	import com.firemoss.ui.blanket.IBlanketOverlay;
	
	import flash.events.Event;
	import flash.geom.Point;
	
	import mx.effects.Fade;
	
	public class LoadingSpinnerDisplay extends AbstractBlanketOverlay
	{
		
		public var spinner : Spinner
		
		public var disableTargetWhenVisible : Boolean = true
		
		public var spinnerColor : uint = 0x999999
		
		public function LoadingSpinnerDisplay()
		{
			
			showEffect = new Fade()
			showEffect.duration = 500
			showEffect.target = this
			Fade(showEffect).alphaFrom = 0
			Fade(showEffect).alphaTo = 1
			
			hideEffect = new Fade()
			hideEffect.duration = 500
			hideEffect.target = this
			Fade(hideEffect).alphaFrom = 1
			Fade(hideEffect).alphaTo = 0
		}

		override protected function createChildren():void
		{
			spinner = new Spinner( 12, 6, spinnerColor )
			
			addChild( spinner )
			
			super.createChildren()
		}
		override public function show( immediately : Boolean = false ):IBlanketOverlay
		{
			var result : IBlanketOverlay = super.show()
			
			if ( disableTargetWhenVisible )
			{
				target.enabled = false
				trace( "disabling " + target.toString() )
			}
			
			//spinner.play()

			return result
		}
		
		override protected function hideImpl( event : Event = null ):void
		{
			super.hideImpl( event )
			
			if ( disableTargetWhenVisible )
			{
				target.enabled = true
				trace( "enabling " + target.toString() )
			}
			
			//spinner.stop()
		}
		
		override protected function invalidatePosition() : void
		{
			if ( target )
			{
				var targetTopLeft : Point = target.localToGlobal( new Point( target.x, target.y ) )
				targetTopLeft.x -= target.x
				targetTopLeft.y -= target.y
			
				x = targetTopLeft.x + target.width / 2 - spinner.radius / 2
				y = targetTopLeft.y + target.height / 2 - spinner.radius / 2
			}
		}
	}
}